package org.example.kidscode.controller;

import org.example.kidscode.entity.PaymentPackage;
import org.example.kidscode.service.PaymentPackageService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/packages")
public class PaymentPackageController {

    private final PaymentPackageService service;

    public PaymentPackageController(PaymentPackageService service) {
        this.service = service;
    }

    @GetMapping
    public List<PaymentPackage> getAll() {
        return service.findAll();
    }

    @GetMapping("/{id}")
    public PaymentPackage getById(@PathVariable String id) {
        return service.findById(id).orElse(null);
    }

    @PostMapping
    public PaymentPackage create(@RequestBody PaymentPackage pkg) {
        return service.save(pkg);
    }

    @PutMapping("/{id}")
    public PaymentPackage update(@PathVariable String id, @RequestBody PaymentPackage pkg) {
        pkg.setPackageId(id);
        return service.save(pkg);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        service.deleteById(id);
    }
}

